home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / m2 / m2_part1.lha / modula / EXTENSIONS < prev    next >
Text File  |  1994-07-30  |  12KB  |  375 lines

  1.     The compiler has been extended with extra features. Most of which are
  2.     designed to make interfacing with the amiga operating system easier.
  3.     Other features are designed to make programming in Modula-2 less annoying.
  4.     Avoid using these extensions if you wish to develop portable modules.
  5.  
  6. Underscore
  7. ==========
  8.  
  9.     The underscore is a valid 'letter.
  10.     eg, '_Turbo_Modula_2' is a legal identifier.
  11.  
  12. SHORTCARD, SHORTINT AND SHORTREAL
  13. =================================
  14.  
  15.     The types SHORTINT, SHORTCARD & SHORTREAL are not normally
  16.     predeclared in other Modula-2 compilers:
  17.  
  18.     SHORTCARD is predeclared as [0..255]    (* byte sized cardinal *)
  19.     SHORTINT  is predeclared as [-128..127] (* byte sized integer  *)
  20.     SHORTREAL Motorola fast floating point.
  21.  
  22. Conversion Rules
  23. ================
  24.  
  25.     Signed (integer) and unsigned(cardinal) numeric subrange types may be freely
  26.     mixed in expressions. The following conversion rules apply:
  27.  
  28.     VAR
  29.       shortint  : SHORTINT  ;
  30.       shortcard : SHORTCARD ;
  31.       integer   : INTEGER ;
  32.       ...
  33.  
  34.     shortint+shortcard   => VAL(INTEGER,shortint)+VAL(INTEGER,shortcard)
  35.     shortint+cardinal    => VAL(LONGINT,shortint)+VAL(LONGINT,cardinal)
  36.  
  37.     shortint+integer     => VAL(INTEGER,shortint)+integer
  38.     shortint+longint     => VAL(LONGINT,shortint)+longint
  39.  
  40.     shortcard+cardinal     => VAL(CARDINAL,shortcard)+cardinal
  41.     shortcard+integer     => VAL(INTEGER,shortcard)+integer
  42.     shortcard+longint    => VAL(LONGINT,shortcard)+longint
  43.  
  44.     integer+cardinal     => VAL(LONGINT,integer)+VAL(LONGINT,cardinal)
  45.  
  46.     integer+longint     => VAL(LONGINT,integer)+longint
  47.  
  48.     cardinal+longint     => VAL(LONGINT,cardinal)+longint
  49.  
  50.     Where '+' could be any binary operator.
  51.     The result type in each conversion should be obvious.
  52.  
  53. Qualified Import aliasing
  54. =========================
  55.  
  56.     An imported global module name may be aliased.
  57.  
  58.     IMPORT Graphics , Intuition ;
  59.  
  60.     VAR
  61.       rp  : Graphics.RastPortPtr ;
  62.       win : Intuition.WindowPtr ;
  63.  
  64.     Can be rewritten:
  65.  
  66.     IMPORT G := Graphics , I := Intuition ;
  67.  
  68.     VAR
  69.       rp  : G.RastPortPtr ;
  70.       win : I.WindowPtr ;
  71.  
  72. Imported library versions
  73. =========================
  74.  
  75.     When importing an Amiga library module ( Dos, Intuition, Graphics etc ),
  76.     an optional version number can be specified:
  77.  
  78.     IMPORT Intuition{33} ;
  79.  
  80.     FROM Dos{36} IMPORT System ;
  81.  
  82.     The values 33 & 36 will be passed to Exec.OpenLibrary by Intuition.o & Dos.o
  83.     If no version number is specified, zero is assumed.
  84.  
  85.     Intuition.o, Dos.o etc will cache the highest version opened so far
  86.     in order to reduce calls to Exec.OpenLibrary.
  87.  
  88.     The version number must be a decimal literal (not an arbitrary expression)
  89.     this simplifies the parser in programs like M2B.
  90.  
  91. Structure assignment
  92. ====================
  93.  
  94.     It is possible to assign to an array or record structure in a single
  95.     statement, the right hand side being a list of bracketed expressions.
  96.  
  97.     The syntax of the assignment statement is extended to:
  98.  
  99.     $ assignment  =  designator ":=" ass_rhs .
  100.     $ ass_rhs  = "["[asslist]"]" | expression.
  101.     $ asslist = ass_rhs{,ass_rhs}
  102.  
  103.     VAR x : ARRAY [0..6] OF INTEGER ;
  104.  
  105.     x := [f(y),2,4,a,0,0,0] ; (* The expressions need NOT be constant *)
  106.  
  107.     Trailing elements/fields need not be assigned to,in which case they will
  108.     be zeroed.Any automatically inserted compiler padding fields will also be 0.
  109.  
  110.     x := [f(y),2,4,a] ; (* same as above *)
  111.  
  112.     If a record contains variant fields then the compiler assumes (and checks
  113.     against) the first variant.In other words you can only assign to the
  114.     first variant field(s).
  115.  
  116.     examples:
  117.  
  118.     VAR
  119.       matrix  = ARRAY [0..3],[0..2],[0..2] OF LONGINT  ;
  120.  
  121.     PROCEDURE InitMatrix ;
  122.     BEGIN
  123.       matrix:=[[[7FFFH,   0,   0],[    0,7FFFH,   0],[    0,    0,7FFFH]],
  124.            [[32642,   0,2856],[    0,7FFFH,   0],[-2856,    0,32642]],
  125.            [[32642,2856,   0],[-2856,32642,   0],[    0,    0,7FFFH]],
  126.            [[7FFFH,   0,   0],[    0,32642,2856],[    0,-2856,32642]]] ;
  127.     END InitMatrix ;
  128.  
  129.     TYPE
  130.       NewMenu = RECORD (* Gadtools structure *)
  131.         nm_Type         : SHORTCARD ;
  132.         CASE : INTEGER OF
  133.         | 0 : nm_Label     : STRING   ;
  134.         | 1 : nm_Image     : ImagePtr ;
  135.         END ;
  136.         nm_CommKey     : STRING  ;
  137.         nm_Flags     : BITSET  ;
  138.         nm_MutualExclude : LONGINT ;
  139.         nm_UserData     : ADDRESS ;
  140.       END ;
  141.  
  142.     VAR
  143.       demomenu : ARRAY [0..11] OF NewMenu ;
  144.  
  145.     BEGIN
  146.       demomenu :=
  147.          [
  148.           [ NM_TITLE,"Project" ],
  149.       [ NM_ITEM, "Run",               "R", {}, 0, MENU_RUN   ],
  150.       [ NM_ITEM, "Step",              "S", {}, 0, MENU_STEP  ],
  151.       [ NM_ITEM, NM_BARLABEL ],
  152.       [ NM_ITEM, "Slower Horizontal", "1", {}, 0, MENU_HSLOW ],
  153.       [ NM_ITEM, "Faster Horizontal", "2", {}, 0, MENU_HFAST ],
  154.       [ NM_ITEM, "Slower Vertical",   "3", {}, 0, MENU_VSLOW ],
  155.       [ NM_ITEM, "Faster Vertical",   "4", {}, 0, MENU_VFAST ],
  156.       [ NM_ITEM, NM_BARLABEL ],
  157.       [ NM_ITEM, "Quit",              "Q", {}, 0, MENU_QUIT  ],
  158.        [ NM_END ]
  159.       ] ;
  160.  
  161. Open array heap variables
  162. =========================
  163.  
  164.     In Modula-2 it is possible to declare open arrays only as formal parameters.
  165.     The compiler allows open arrays to be declared as pointer base types.
  166.  
  167.     VAR
  168.       p : POINTER TO ARRAY OF CHAR ;
  169.  
  170.     Like formal parameters, the open array must be 1 dimensional.
  171.  
  172.     The NEW substitution mechanism has to be extended to allow declarations
  173.     of such heap variables.
  174.  
  175.     NEW(p,exp) ; where exp is an integer/cardinal expression.
  176.  
  177.     This expands to ALLOCATE( p , exp*SIZE(p^[0]));
  178.  
  179.     There is no bounds checking associated with this type of array.
  180.     It is not possible to do any operation on the open array,only individual
  181.     elements may be accessed:
  182.  
  183.       The designator p^ may never appear on its own, the '^' must always be
  184.       followed by a '[' eg p^[10].
  185.  
  186.     If the follow declarations exist:
  187.  
  188.       VAR
  189.         x : POINTER TO ARRAY OF type ;
  190.         y : ARRAY [low..hi] OF type ; (* normal array *)
  191.  
  192.     Then the assignment
  193.  
  194.      x := y ; is allowed. This is equivalent to
  195.      x := ADR( y ) ;
  196.  
  197.      If the base type of the open array(x^[0]) is CHAR then,
  198.  
  199.      x := "help" ;      is allowed and is equivalent to
  200.      x := ADR("help") ;
  201.  
  202.      The SYSTEM.STRING type is predeclared as POINTER TO ARRAY OF CHAR.
  203.      The array pointed to by a STRING variable should always be 0C terminated.
  204.  
  205.  
  206.      This kind of pointer occurs in the C language, and so is quite useful
  207.      when programming with the amiga OS and the Ansi C libraries. However it is
  208.      not true to Modula's strong typing philosophy and therefore its
  209.      indiscriminate use should be avoided.
  210.  
  211. Pointer casting in selector lists
  212. =================================
  213.  
  214.     It is possible to insert type casts in variable selector lists:
  215.  
  216.     foo := bar(NewMenuPtr)^.nm_Label(ImagePtr) ;
  217.  
  218.     The syntax of the designator is extended to:
  219.  
  220.     $ designator = qualident {"."ident|"["ExpList"]"|"("qualident")"| "^" }.
  221.  
  222.     This method of casting is restricted to (and from) pointer types.
  223.     It is very useful when using Intuitions BOOPSI system.
  224.  
  225. Variable length argument lists
  226. ==============================
  227.  
  228.     A procedure may be declared to take a variable number of arguments.
  229.     To declare such a procedure, the formal parameter list must end with '..':
  230.  
  231.     PROCEDURE VarArgsProc( x : INTEGER ; .. ) ;
  232.     (* There must be at least 1 normal formal parameter *)
  233.  
  234.     There is no standard way of accessing the unknown parameters from Modula-2.
  235.     This kind of declaration normally occur in Amiga OS & C interface modules.
  236.     However you can declare them in implementation & program modules:
  237.  
  238.     PROCEDURE CreateGad( kind:LONGINT; VAR ng:GT.NewGadget; tag1:LONGINT; .. ) ;
  239.     (* topazAttr, visualInfo, window, lastAdded are global variables *)
  240.     BEGIN
  241.       ng.ng_TextAttr    := ADR( topazAttr ) ;
  242.       ng.ng_VisualInfo  := visualInfo ;
  243.       INC( ng.ng_LeftEdge, window^.BorderLeft ) ;
  244.       INC( ng.ng_TopEdge , window^.BorderTop  ) ;
  245.  
  246.       lastAdded := GT.CreateGadgetA( kind, lastAdded, ng, ADR( tag1 ) )
  247.     END CreateGad ;
  248.  
  249.     If a SHORTREAL or REAL actual corresponds to a '..' formal
  250.     then the parameter is converted to a LONGREAL before being passed.
  251.  
  252. Newline and tab characters
  253. ==========================
  254.  
  255.     String literals may contain newline (\n) & tab(\t) characters.
  256.  
  257.     InOut.WriteString("Hello\tworld\n");
  258.  
  259.     If '\' in a string is not followed by one of 't' 'n' '\' then the compiler
  260.     will report an error.
  261.     To represent '\' use '\\'.
  262.  
  263.     "\n" & "\t" are also legal character constants as well as strings.
  264.  
  265. Exit codes
  266. ==========
  267.  
  268.     A return statement inside the initialization statement sequence of the root
  269.     program module may contain an optional integer expression. This expression,
  270.     if executed, will represent the return code for the program.
  271.     If no expression is supplied, or if execution falls through, 0 is returned.
  272.  
  273.     MODULE foo ;
  274.      ...
  275.     BEGIN
  276.       RETURN 20
  277.     END foo.
  278.  
  279.     The standard AmigaDOS return codes are:
  280.  
  281.     00: program was successful.
  282.     10: warning (non fatal 'error').
  283.     20: fatal error.
  284.  
  285.     Alternatively StdLib.exit(X), will terminate the program with return code X.
  286.  
  287. Coroutines
  288. ==========
  289.  
  290.     The pseudo module SYSTEM does not contain any coroutine facilities,
  291.     instead the library module 'Coroutines' should be used.
  292.  
  293. SHORTSET BITSET & LONGSET constants
  294. ===================================
  295.  
  296.     The compiler predefines 3 set types.
  297.  
  298.     SHORTSET = SET OF [0..07] ; SIZE = 1 Byte
  299.     BITSET   = SET OF [0..15] ; SIZE = 2 Byte
  300.     LONGSET  = SET OF [0..31] ; SIZE = 4 Byte
  301.  
  302.     The type of an unqualified set constant has been extended to support these
  303.     types.
  304.  
  305.        {}, {0}, {0,1} ... {0..7} are compatible with SHORTSET,BITSET & LONGSET
  306.        {8}, {8,9} ... {8..15} are compatible with BITSET & LONGSET
  307.        {16} .. {16..31} are only LONGSETs
  308.  
  309.        By compatible I mean both expression compatible & assignment compatible.
  310.  
  311.     VAR
  312.       shortset : SHORTSET ; bitset : BITSET ; longset  : LONGSET  ;
  313.  
  314.     shortset:= {1} (*valid*); shortset:= {8} (*wrong*); shortset:= {16}(*wrong*)
  315.     bitset  := {1} (*valid*); bitset  := {8} (*valid*); bitset  := {16}(*wrong*)
  316.     longset := {1} (*valid*); longset := {8} (*valid*); longset := {16}(*valid*)
  317.  
  318. INCL & EXCL
  319. ===========
  320.  
  321.     As well as the normal definitions of the standard procedures INCL/EXCL
  322.     the second parameter of these procedures may be a set expression.
  323.  
  324.     example:
  325.  
  326.       INCL(bitset,{1,2}) is identical to bitset := bitset+{1,2}.
  327.       EXCL(bitset,{1,2}) is identical to bitset := bitset-{1,2}.
  328.  
  329.       However the INCL/EXCL versions generate atomic code.
  330.  
  331. BCPL pointers
  332. =============
  333.  
  334.      The DOS operating subsystem was partly implemented in the BCPL programming
  335.      language, which used long word pointers,as opposed to normal byte pointers.
  336.      It is possible to declare a BCPL long word pointer in Turbo Modula-2
  337.  
  338.      TYPE
  339.        FileLockPtr = BCPL POINTER TO FileLock ; (* BCPL is a reserved keyword*)
  340.  
  341.      Instances of this pointer can be dereferenced as normal.
  342.  
  343.      The type SYSTEM.BADDRESS exists and is analogous to the normal
  344.      SYSTEM.ADDRESS type.
  345.  
  346.      If an assignment to a BCPL pointer from an ADDRESS typed expression occurs
  347.      then the compiler will automatically perform the necessary conversion:
  348.  
  349.      VAR lock : FileLockPtr ; v : FileLock ;
  350.  
  351.      lock := ADR(v) ; (* will work, but only if v is long word aligned *)
  352.  
  353.      The reverse assignment from a BADDRESS expression to a normal pointer will
  354.      also be converted properly.
  355.  
  356.      You must be careful not to cast between the 2 different kinds of pointers
  357.      as no conversion will take place. Instead there are 2 functions in the
  358.      SYSTEM module that can be used.
  359.  
  360.      PROCEDURE SYSTEM.BTOA( bp : BADDRESS ) : ADDRESS ;
  361.      PROCEDURE SYSTEM.ATOB(  p : ADDRESS  ) : BADDRESS ;
  362.  
  363.      The base variable of a BCPL pointer should always be long word aligned.
  364.      Modula-2 global variables are always longword aligned as is the memory
  365.      allocated using the StdLib.malloc & Storage.ALLOCATE functions.
  366.  
  367. SHORTFLOAT & LONGFLOAT
  368. ======================
  369.  
  370.      As well as the normal FLOAT conversion function, which always converts
  371.      to REAL, there are 2 other integer/cardinal->real conversion functions:
  372.  
  373.      SHORTFLOAT, converts an integer/cardinal into a SHORTREAL
  374.      LONGFLOAT , converts an integer/cardinal into a LONGREAL
  375.